﻿#include "dxstdafx.h"
#include "ProgramDataMgr.h"
#include "ProvinceData.h"
#include "StrategyMapData.h"
#include "LandedTitlesData.h"
#include "Strategy/Scene/CStrategyRenderCommon.h"
#include "Strategy/Scene/CStrategyProvinceBorder.h"

extern CStrategyProvinceBorder g_StrategyProvinceBorder;
extern CStrategyRenderData g_StrategyRenderData;

CProgramDataMgr::CProgramDataMgr()
{
	try
	{
		m_pProvinceData = new CProvinceData();
		m_pStrategyMapData = new CStrategyMapData();
		m_pLandedTitlesData = new CLandedTitlesData();
	}
	catch (std::bad_alloc &memExp)
	{
		abort();
	}
}

CProgramDataMgr::~CProgramDataMgr()
{
	SAFE_DELETE(m_pProvinceData);
	SAFE_DELETE(m_pStrategyMapData);
	SAFE_DELETE(m_pLandedTitlesData);
}

void CProgramDataMgr::InitData()
{
	m_pStrategyMapData->ReadHeightMap();
	
	{
		m_pStrategyMapData->CreateTerrainData();
		g_StrategyRenderData.TransformTerrainMap(m_pStrategyMapData->GetTerrainBufCPtr());
	}

	{
		_stRGB* pProvinceRGB = NULL;
		m_pProvinceData->CreateProvinceData(m_pStrategyMapData->GetTerrainBufCPtr(), pProvinceRGB);
		g_StrategyProvinceBorder.CreateProvinceBorderInfo(pProvinceRGB);
		SAFE_DELETE_ARRAY(pProvinceRGB);
		m_pProvinceData->TidyProvinceTerrain();
	}
	{
		m_pLandedTitlesData->CreateDataAfterLandedTitlesPropertyRead();
		m_pProvinceData->LinkLandedTitleNProvinceData();
		m_pProvinceData->LinkPositions2ProvinceData();
	}

	if (!IsDataComplete())
	{
		CString strError("Strategy Data Isn't Complete!");
		CommonFunc::ErrorMessageBox(strError);
	}
}

bool CProgramDataMgr::IsDataComplete() const
{
	return m_pProvinceData->IsDataComplete();
}